package com.foreveross.dao.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.ExpressDAO;
import com.foreveross.vo.Express;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Component("expressDAO")
public class ExpressDAOImp extends SQLUtil implements ExpressDAO {

	@Override
	public List<Express> findExpressList(String express_no,
			String house_user_name, String name, String type, String status,
			String house_id, String telephone, int pageIndex, int pageSize,String userId,String resCode)
			throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from Express model  where 1=1 ");
		if (!"".equals(express_no) && null != express_no) {
			queryString.append(" and model.express_no like '%" + express_no
					+ "%'");
		}
		if (!"".equals(house_user_name) && null != house_user_name) {
			queryString.append(" and model.houseUser.name like '%"
					+ house_user_name + "%'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type=" + type);
		}

		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status=" + status);
		}

		if (!"".equals(house_id) && null != house_id) {
			queryString.append(" and model.houseUser.hrHouses.id='" + house_id
					+ "'");
		}
		if (!"".equals(telephone) && null != telephone) {
			queryString.append(" and model.telephone like '%" + telephone
					+ "%'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%"
					+ resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseUser.hrHouses.id");
		queryString.append(" order by model.id desc");
		return (List<Express>) findObjectAll(queryString.toString(), pageIndex,
				pageSize);
	}

	@Override
	public long getExpressListCount(String express_no, String house_user_name,
			String name, String type, String status, String house_id,
			String telephone,String userId,String resCode) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from Express model  where 1=1 ");
		if (!"".equals(express_no) && null != express_no) {
			queryString.append(" and model.expressNo like '%" + express_no
					+ "%'");
		}
		if (!"".equals(house_user_name) && null != house_user_name) {
			queryString.append(" and model.houseUser.name like '%"
					+ house_user_name + "%'");
		}
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type=" + type);
		}

		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status=" + status);
		}

		if (!"".equals(house_id) && null != house_id) {
			queryString.append(" and model.houseUser.hrHouses.id='" + house_id
					+ "'");
		}

		if (!"".equals(telephone) && null != telephone) {
			queryString.append(" and model.telephone like '%" + telephone
					+ "%'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%"
					+ resCode + "%'");
		}
		this.addAuthorityStr(queryString, userId, "houseUser.hrHouses.id");
		return (Long) findObjectAll(queryString.toString()).get(0);
	}
	
	
	public int findUntreatedTotal(String weixin, String type,
			String currentPage, String pageSize) throws Exception {
		String sqlfrom = "select model ";
		StringBuffer queryString = new StringBuffer(
				" from Express model where 1=1 ");
		if (!"".equals(weixin) && null != weixin) {
			queryString.append(" and model.houseUser.wechatId= '" + weixin
					+ "'");
		}
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type in (" + type + " )");
		}
		if ("2".equals(type))
		  return 	 Integer.parseInt(findObjectAll(
					"select count(*)" + queryString.toString()
							+ " and model.status=" + 4).get(0).toString());
		else
			return Integer.parseInt(findObjectAll(
					"select count(*)" + queryString.toString()
							+ " and model.status in (1,6)").get(0).toString());
	}

	@Override
	public PageVo<Express> findMyExpressList(String weixin, String type,
			String currentPage, String pageSize) throws Exception {
		PageVo<Express> page = new PageVo<Express>();
		String sqlfrom = "select model ";
		StringBuffer queryString = new StringBuffer(
				" from Express model where 1=1 ");
		if (!"".equals(weixin) && null != weixin) {
			queryString.append(" and model.houseUser.wechatId= '" + weixin
					+ "'");
		}
		if (!"".equals(type) && null != type) {
			queryString.append(" and model.type in (" + type + " )");
		}

		page.settotal(Integer.parseInt(findObjectAll(
				"select count(*)" + queryString.toString()).get(0).toString()));
		queryString.append(" order by model.id desc");
		if (currentPage != null && !"".equals(currentPage)
				&& currentPage != null && !"".equals(currentPage)) {
			page.init(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
			page.setData((List<Express>) findObjectAll(
					sqlfrom + queryString.toString(), page.getpageIndex(),
					Integer.parseInt(pageSize)));
		} else {
			page.setData((List<Express>) findObjectAll(sqlfrom
					+ queryString.toString()));
		}
		return page;
	}
	//

}
